package com.example.mobile2021606134chhnewdemo.LoginModule;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.mobile2021606134chhnewdemo.ContactModule.ChatBean;
import com.example.mobile2021606134chhnewdemo.ContactModule.SpeedRobot;
import com.example.mobile2021606134chhnewdemo.NewsModule.NewsActivity;
import com.example.mobile2021606134chhnewdemo.R;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.Random;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class LoginActivity extends AppCompatActivity {

    private ImageView imgLoginUserImg;
    private EditText edtPhone;
    private EditText edtPhoneNum;
    private Button btnLoginLogin;
    private Button btnLoginRegister;
    private TextView tvLoginRetrievePassword;
    private TextView tvLoginRetrieveAccount;
    private ImageView LogoImage;
    private TextView TitleText;
    private LinearLayout UserNameLayout;
    private LinearLayout PassWordLayout;
    private Button btnGetNum;
    private LinearLayout ButtonLayout;
    private String code;
    private static final String smsSignId = "2e65b1bb3d054466b82f0c9d125465e2";
    private static final String templateId = "908e94ccf08b4476ba6c876d13f084ad";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_login);
        initView();

        imgLoginUserImg.setImageResource(R.drawable.new_s);   //默认为无用户头像
        code = String.format("%04d",new Random().nextInt(9999));
        Log.i("TAG", "验证码:" + code);

        btnGetNum.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("DefaultLocale")
            @Override
            public void onClick(View v) {
                String phone = edtPhone.getText().toString().trim();
                OkHttpClient okHttpClient = new OkHttpClient();
                RequestBody requestBody = new FormBody.Builder()
                        .add("mobile", phone)
                        .add("param", "**code**:" + code + ",**minute**:5")
                        .add("smsSignId", smsSignId)
                        .add("templateId", templateId)
                        .build();
                Request request = new Request.Builder()
                        .addHeader("Authorization", "APPCODE " + Login.APPCODE)
                        .url(Login.URL)
                        .post(requestBody)
                        .build();
                Call call = okHttpClient.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String result = response.body().string();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Log.i("TAG", result);
                            }
                        });

                    }
                    @Override
                    public void onFailure(Call call, IOException e) {
                    }
                });
            }
        });

        btnLoginLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String getCode = edtPhoneNum.getText().toString().trim();
                if (getCode.equals(code)) {
                    Intent intent = new Intent(getApplicationContext(), NewsActivity.class);
                    startActivity(intent);
                    finish();
                    Toast.makeText(LoginActivity.this, "登录成功！", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(LoginActivity.this, "请输入正确的验证码！", Toast.LENGTH_SHORT).show();
                }
            }
        });
        btnLoginRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), RegisterActivity01.class);
                startActivity(intent);
            }
        });
        tvLoginRetrieveAccount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), RetrieveAccountActivity.class);
                startActivity(intent);
            }
        });
        tvLoginRetrievePassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), RetrievePasswordActivity.class);
                startActivity(intent);
            }
        });


    }

    private void initView() {
        imgLoginUserImg = (ImageView) findViewById(R.id.LogoImage);
        edtPhone = (EditText) findViewById(R.id.edtPhone);
        edtPhoneNum = (EditText) findViewById(R.id.edtPhoneNum);
        btnLoginLogin = (Button) findViewById(R.id.btnLoginLogin);
        btnLoginRegister = (Button) findViewById(R.id.btnLoginRegister);
        tvLoginRetrievePassword = (TextView) findViewById(R.id.tvLoginRetrievePassword);
        tvLoginRetrieveAccount = (TextView) findViewById(R.id.tvLoginRetrieveAccount);
        LogoImage = (ImageView) findViewById(R.id.LogoImage);
        TitleText = (TextView) findViewById(R.id.TitleText);
        UserNameLayout = (LinearLayout) findViewById(R.id.UserNameLayout);
        PassWordLayout = (LinearLayout) findViewById(R.id.PassWordLayout);
        btnGetNum = (Button) findViewById(R.id.btnGetNum);
        ButtonLayout = (LinearLayout) findViewById(R.id.ButtonLayout);
    }
}